package com.itheima.project.face.impl;

import com.itheima.project.pojo.ContentBaseRule;
import com.itheima.project.service.IContentBaseRuleService;
import com.itheima.project.vo.content.ContentBaseRuleVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.face.ContentBaseRuleFace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.constant.content.ContentBaseRuleCacheConstant;
import com.itheima.project.core.enums.content.ContentBaseRuleEnum;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
 * @Description：Face服务实现类
 */
@Slf4j
@Component
public class ContentBaseRuleFaceImpl implements ContentBaseRuleFace {

    @Autowired
    IContentBaseRuleService contentBaseRuleService;


    @Override
    @Cacheable(value = ContentBaseRuleCacheConstant.PAGE,key ="#pageNum+'-'+#pageSize+'-'+#contentBaseRuleVo.hashCode()")
    public Page<ContentBaseRuleVo> findContentBaseRuleVoPage(ContentBaseRuleVo contentBaseRuleVo, int pageNum, int pageSize) {
        try {
            Page<ContentBaseRule> page = contentBaseRuleService.findContentBaseRulePage(contentBaseRuleVo, pageNum, pageSize);
            Page<ContentBaseRuleVo> pageVo =BeanConv.toPage(page,ContentBaseRuleVo.class);
            return pageVo;
        }catch (Exception e){
            log.error("列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentBaseRuleEnum.PAGE_FAIL);
        }
    }

    @Transactional
    @Caching(evict = {@CacheEvict(value = ContentBaseRuleCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ContentBaseRuleCacheConstant.LIST,allEntries = true)},
    put={@CachePut(value =ContentBaseRuleCacheConstant.BASIC,key = "#result.id")})
    @Override
    public ContentBaseRuleVo createContentBaseRuleVo(ContentBaseRuleVo contentBaseRuleVo) {
        try {
            ContentBaseRuleVo contentBaseRuleVoResult = BeanConv.toBean(contentBaseRuleService.createContentBaseRule(contentBaseRuleVo), ContentBaseRuleVo.class);
            return contentBaseRuleVoResult;
        } catch (Exception e) {
            log.error("保存异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentBaseRuleEnum.SAVE_FAIL);
        }
    }

    @Transactional
    @Caching(evict = {@CacheEvict(value = ContentBaseRuleCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ContentBaseRuleCacheConstant.LIST,allEntries = true),
        @CacheEvict(value =ContentBaseRuleCacheConstant.BASIC,key = "#contentBaseRuleVo.id")})
    @Override
    public Boolean updateContentBaseRuleVo(ContentBaseRuleVo contentBaseRuleVo) {
        try {
            Boolean flag = contentBaseRuleService.updateContentBaseRule(contentBaseRuleVo);
            return flag;
        } catch (Exception e) {
            log.error("修改异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentBaseRuleEnum.UPDATE_FAIL);
        }
    }

    @Transactional
    @Caching(evict = {@CacheEvict(value = ContentBaseRuleCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ContentBaseRuleCacheConstant.LIST,allEntries = true)})
    @Override
    public Boolean deleteContentBaseRuleVo(String[] checkedIds) {
        try {
            Boolean flag = contentBaseRuleService.deleteContentBaseRule(checkedIds);
            return flag;
        } catch (Exception e) {
            log.error("删除异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentBaseRuleEnum.DEL_FAIL);
        }
    }

    @Override
    @Cacheable(value = ContentBaseRuleCacheConstant.LIST,key ="#contentBaseRuleVo.hashCode()")
    public List<ContentBaseRuleVo> findContentBaseRuleVoList(ContentBaseRuleVo contentBaseRuleVo) {
        try {
            List<ContentBaseRuleVo> records = BeanConv.toBeanList(contentBaseRuleService.findContentBaseRuleList(contentBaseRuleVo),ContentBaseRuleVo.class);
            return records;
        } catch (Exception e) {
            log.error("删除异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentBaseRuleEnum.LIST_FAIL);
        }
    }
}
